import {reqgoodslist,reqgoodscount} from '../../http/api'
let state={
    list:[],
    total:0,
    page:1,
    size:2,
};
let getters={
    list(state){
        return state.list;
    },
    page(state){
        return state.page;
    },
    size(state){
        return state.size;
    },
    total(state){
        return state.total;
    },
    
};
let mutations={
    changeList(state,list){
        state.list = list;
    },
    changePage(state,page){
        state.page = page;
    },
    changeSize(state,size){
        state.size = size;
    },
    changeTotal(state,total){
        state.total = total;
    },

};
let actions={
    async getList({commit,dispatch,state:{size,page}}){
        let {data:res} =await reqgoodslist( {size,page});
        if(res.code == 200){
            let arr = res.list ? res.list : [];
            if(arr.length == 0 && page!=1){
                commit('changePage',page-1);
                dispatch('getList');
                return;
            }
            commit('changeList',arr);
            dispatch('getTotal');
        }
    },
    async getTotal({commit}){
        let {data:res} = await reqgoodscount();
        if(res.code == 200){
            commit('changeTotal',res.list[0].total);
        }
    },
    changePage({dispatch,commit},e){
        commit('changePage',e);
        dispatch('getList');
    }
};
export default{
    state,
    actions,
    mutations,
    getters,
    namespaced: true
}